package com.wyg.epuser.utils.authUtil.gitee;

import com.alibaba.fastjson.JSONObject;
import com.wyg.epuser.utils.authUtil.AuthCallBack;
import com.wyg.epuser.utils.authUtil.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

/**
 * @Author: WangYongGan
 * @Date: 2022/4/25 20:33
 * @Description:
 */
@Component
@Slf4j
public class GiteeAuth {
    @Autowired
    RestTemplate restTemplate;
    public static String codeUrlBase = "https://gitee.com/oauth/authorize?response_type=code";
    public static String tokenUrlBase = "https://gitee.com/oauth/token?";
    public static String userUrlBase = "https://gitee.com/api/v5/user?";
    public static String client_id = "e2c295ef1c1aa096e4f0d2a30590ef91e35d3a7e2971acdf82fc837b55439fb1";
    public static String client_secret = "4cda46d4432bf77dd865002c91eb8232756607f60fb8bfb2f46cf0f92d4339ac";
    public static String redirect_uri = "http://114.55.253.244:8001/auth/backGitee";
    //    public static String redirect_uri = "http://localhost:8001/auth/backGitee";
    public static String scopeType = "user_info";
    public static String tokenType = "access_token";
    public static String mainUri = "http://114.55.253.244/login";

    public String createAuthCode(String id) {
        return "gitee_id:" + id;
    }

    public AuthUser getUser(AuthCallBack auth) {
        final String tokenUrl = urlForToken(auth.getCode());
        final String tokenObject = restTemplate.postForObject(tokenUrl, null, String.class);
        final String token = JSONObject.parseObject(tokenObject).getString(tokenType);
        log.info("token::" + token);
        final String userUrl = urlForUser(token);
        final String userObject = restTemplate.getForObject(userUrl, String.class);
        log.info("user:" + userObject);
        AuthUser authUser = new AuthUser();
        final JSONObject userJson = JSONObject.parseObject(userObject);
        final Integer id = (Integer) userJson.get("id");
        authUser.setId(String.valueOf(id));
        authUser.setUsername((String) userJson.get("login"));
        return authUser;
    }

    public String getUUID() {
        return UUID.randomUUID().toString();
    }

    public String urlForCode(String uuid) {
        StringBuilder authUrl = new StringBuilder();
        base(codeUrlBase, authUrl);
        authUrl.append("state=");
        authUrl.append(uuid);
        authUrl.append("&");
        authUrl.append("scope=");
        authUrl.append(scopeType);
        return authUrl.toString();
    }

    public String urlForToken(String code) {
        StringBuilder url = new StringBuilder(tokenUrlBase);
        url.append("code=");
        base(code, url);
        url.append("grant_type=authorization_code");
        return url.toString();
    }

    public String urlForUser(String token) {
        StringBuilder url = new StringBuilder(userUrlBase);
        url.append("access_token=");
        url.append(token);
        return url.toString();
    }

    private void base(String code, StringBuilder url) {
        url.append(code);
        url.append("&");
        url.append("client_id=");
        url.append(client_id);
        url.append("&");
        url.append("client_secret=");
        url.append(client_secret);
        url.append("&");
        url.append("redirect_uri=");
        url.append(redirect_uri);
        url.append("&");
    }
}
